<?php

namespace App\Services\Warehouse\Services;

use App\Services\Warehouse\Interfaces\InventoryManagementServiceInterface;
use App\Services\Warehouse\Model\Parts;
use App\Services\Warehouse\Model\PartsInventory;
use App\Services\Warehouse\Model\PartsOperation;
use App\Services\Warehouse\Model\FirstAuditReceive;
use App\Services\Warehouse\Model\Equipment;
use App\Services\Warehouse\Model\FreightSpace;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Exceptions\ApiV2Exception;
use Illuminate\Support\Carbon;

class InventoryManagementService implements InventoryManagementServiceInterface
{
    public function in_warehouse($request, $id){
        $id = $this->getpartsid($id);
        $Inventory = PartsInventory::findOrFail($id);
        if(empty($Inventory)){
            throw new ApiV2Exception("配件不存在，不能申请入仓");
        }
        if($Inventory['state'] == PartsInventory::STATE_SCRAP){
            throw new ApiV2Exception("配件已经报废，不能入仓");
        }
        if($Inventory['state'] == PartsInventory::STATE_NEW_PRODUCT){
            throw new ApiV2Exception("配件是新品，审核后才能操作");
        }
        $fs = FreightSpace::query()->where('position','=',$request['position'])->first();
        if(empty($fs)){
            throw new ApiV2Exception("仓位不存在");
        }
        $fs_id = $fs['id'];
        $partfs = PartsInventory::query()->where('fs_id','=',$fs_id)->first();
        if(!empty($partfs)){
            throw new ApiV2Exception("仓位已经有配件，不能入仓");
        }
        $uid = Auth::id();
        DB::beginTransaction(); 
        try{
            $state_Operation = 0;
            $state_Inventory = PartsInventory::STATE_CANGKU;
            $description = '';
            if($Inventory['state'] == PartsInventory::STATE_SET_UP){
                $state_Operation = PartsOperation::STATE_RUKU;
                $description = '新品入库';
                $state_Inventory = PartsOperation::STATE_NEW_PRODUCT;
            }
            if($Inventory['state'] == PartsInventory::STATE_EMPLOY){
                $state_Operation = PartsOperation::STATE_GIVE_BACK;
                $description = '配件归还';
            }
            if($Inventory['state'] == PartsInventory::STATE_MAINTAIN){
                $state_Operation = PartsOperation::STATE_COME_BACK_REPAIR;
                $description = '修模归还';
            }
            if($Inventory['state'] == PartsInventory::STATE_CANGKU || $Inventory['state'] == PartsInventory::STATE_NEED_REPAIR){
                $state_Operation = PartsOperation::STATE_AMEND_POSITION;
                $description = '调仓';
            }
            $PartsOperation = new PartsOperation();
            $OperationData = array('parts_id'=>$id,'description'=>$description,'user_id'=>$uid,'state'=>$state_Operation);
            $PartsOperation->fill($OperationData)->save();

            $InventoryData = PartsInventory::findOrFail($id);
            $InventoryData->fill(array('fs_id'=>$fs_id,'state' => $state_Inventory,'po_id'=>$PartsOperation['id'],'user_id'=>$uid))->save();
            DB::commit();
        } catch (\Exception $e) {
            DB::rollback(); 
            throw $e;
        }
        return;
    }

    //领用
    public function receive($request, $id)
    {
        $id = $this->getpartsid($id);
        $Inventory = PartsInventory::query()->where('parts_id','=',$id)->first();
        if(empty($Inventory)){
            throw new ApiV2Exception("配件不存在，不能操作");
        }
        if($Inventory['state'] == PartsInventory::STATE_SCRAP){
            throw new ApiV2Exception("配件已经报废，不能操作");
        }
        if($Inventory['state'] == PartsInventory::STATE_NEW_PRODUCT){
            throw new ApiV2Exception("配件是新品，审核后才能操作");
        }
        if($Inventory['state'] == PartsInventory::STATE_NEED_REPAIR){
            throw new ApiV2Exception("配件需要送修摸，不能领用");
        }
        if($Inventory['state'] != PartsInventory::STATE_CANGKU){
            throw new ApiV2Exception("配件不在仓库，不能领用");
        }
        
        $Equipmentdata = Equipment::query()->where('id','=',$request['equipment_id'])->first();
        if(empty($Equipmentdata)){
            throw new ApiV2Exception("使用设备不存在，不能领用");  
        }
        $uid = Auth::id();
        DB::beginTransaction(); 
        try{
            $PartsOperation = new PartsOperation();
            $OperationData = array('parts_id'=>$id,'description'=>'配件领用','user_id'=>$uid,'state'=>PartsOperation::STATE_RECEIVE,'equipment_id'=>$request['equipment_id']);
            $PartsOperation->fill($OperationData)->save();

            $InventoryData = PartsInventory::findOrFail($id);
            $InventoryData->fill(array('fs_id'=>0,'state' => PartsInventory::STATE_EMPLOY,'po_id'=>$PartsOperation['id'],'user_id'=>$uid))->save();
            DB::commit();
        } catch (\Exception $e) {
            DB::rollback(); 
            throw $e;
        }
        return;
    }

    public function need_repair($request, $id)
    {
        $id = $this->getpartsid($id);
        $Inventory = PartsInventory::query()->where('parts_id','=',$id)->first();
        if(empty($Inventory)){
            throw new ApiV2Exception("配件不存在，不能操作");
        }
        if($Inventory['state'] == PartsInventory::STATE_SCRAP){
            throw new ApiV2Exception("配件已经报废，不能操作");
        }
        if($Inventory['state'] == PartsInventory::STATE_NEW_PRODUCT){
            throw new ApiV2Exception("配件是新品，审核后才能操作");
        }
        if($Inventory['state'] != PartsInventory::STATE_CANGKU){
            throw new ApiV2Exception("配件不在仓库，不能设置需要修模");
        }
        
        $uid = Auth::id();
        DB::beginTransaction(); 
        try{
            $PartsOperation = new PartsOperation();
            $OperationData = array('parts_id'=>$id,'description'=>'设置需要修模','user_id'=>$uid,'state'=>PartsOperation::STATE_NEED_REPAIR);
            $PartsOperation->fill($OperationData)->save();

            $InventoryData = PartsInventory::findOrFail($id);
            $InventoryData->fill(array('fs_id'=>0,'state' => PartsInventory::STATE_NEED_REPAIR,'po_id'=>$PartsOperation['id'],'user_id'=>$uid))->save();
            DB::commit();
        } catch (\Exception $e) {
            DB::rollback();
            throw $e;
        }
        return;
    }

    //外出修模
    public function out_repair($request, $id)
    {
        $id = $this->getpartsid($id);
        $Inventory = PartsInventory::query()->where('parts_id','=',$id)->first();
        if(empty($Inventory)){
            throw new ApiV2Exception("配件不存在，不能操作");
        }
        if($Inventory['state'] == PartsInventory::STATE_SCRAP){
            throw new ApiV2Exception("配件已经报废，不能操作");
        }
        if($Inventory['state'] == PartsInventory::STATE_NEW_PRODUCT){
            throw new ApiV2Exception("配件是新品，审核后才能操作");
        }
        if($Inventory['state'] != PartsInventory::STATE_NEED_REPAIR){
            if($Inventory['state'] != PartsInventory::STATE_CANGKU){
                throw new ApiV2Exception("配件不在仓库，不能外出修模");
            }
        }
        
        $uid = Auth::id();
        DB::beginTransaction(); 
        try{
            $PartsOperation = new PartsOperation();
            $OperationData = array('parts_id'=>$id,'description'=>'配件送修模','user_id'=>$uid,'state'=>PartsOperation::STATE_OUT_REPAIR);
            $PartsOperation->fill($OperationData)->save();

            $InventoryData = PartsInventory::findOrFail($id);
            $InventoryData->fill(array('fs_id'=>0,'state' => PartsInventory::STATE_MAINTAIN,'po_id'=>$PartsOperation['id'],'user_id'=>$uid))->save();
            DB::commit();
        } catch (\Exception $e) {
            DB::rollback();
            throw $e;
        }
        return;
    }
     //报废
     public function scrap($request, $id)
     {
        $id = $this->getpartsid($id);
        $Inventory = PartsInventory::query()->where('parts_id','=',$id)->first();
        if(empty($Inventory)){
            throw new ApiV2Exception("配件不存在，不能操作");
        }
        if($Inventory['state'] == PartsInventory::STATE_SCRAP){
            throw new ApiV2Exception("配件已经报废，不能操作");
        }
        if($Inventory['state'] == PartsInventory::STATE_NEW_PRODUCT){
            throw new ApiV2Exception("配件是新品，审核后才能操作");
        }
        if($Inventory['state'] != PartsInventory::STATE_CANGKU){
            throw new ApiV2Exception("配件不在仓库，不能报废");
        }

        $uid = Auth::id();
        DB::beginTransaction(); 
        try{
            $PartsOperation = new PartsOperation();
            $OperationData = array('parts_id'=>$id,'description'=>'配件报废','user_id'=>$uid,'state'=>PartsOperation::STATE_SCRAP);
            $PartsOperation->fill($OperationData)->save();

            $InventoryData = PartsInventory::findOrFail($id);
            $InventoryData->fill(array('fs_id'=>0,'state' => PartsInventory::STATE_SCRAP,'po_id'=>$PartsOperation['id'],'user_id'=>$uid))->save();
            
            $Parts = Parts::findOrFail($id);
            $Parts->fill(array('isScrap'=>Parts::SCRAP))->save();
            DB::commit();
        } catch (\Exception $e) {
            DB::rollback();
            throw $e;
        }
        return;
     }
    //申请新品审核
    public function apply_first_audit_receive($request, $id){
        $id = $this->getpartsid($id);
        $Inventory = PartsInventory::query()->where('parts_id','=',$id)->first();
        if(empty($Inventory)){
            throw new ApiV2Exception("配件不存在，不能申请审核");
        }
        if($Inventory['state'] == PartsInventory::STATE_SCRAP){
            throw new ApiV2Exception("配件已经报废，操作失败");
        }
        if($Inventory['state'] != PartsInventory::STATE_NEW_PRODUCT){
            throw new ApiV2Exception("配件不是新品入库，操作失败");
        }
        $data = FirstAuditReceive::query()->where('parts_id','=',$id)->first();
        if(!empty($data)){
            throw new ApiV2Exception("已经申请审核，不要重复申请");
        }
        $uid = Auth::id();
        $FirstAuditReceive = new FirstAuditReceive();
        $AuditReceiveData = array('parts_id'=>$id,'user_id'=>$uid);
        $FirstAuditReceive->fill($AuditReceiveData)->save();
        return;
    }

    //领用审核
    public function first_audit_receive($request, $id)
    {
        $id = $this->getpartsid($id);
        $data = FirstAuditReceive::query()->where('parts_id','=',$id)->first();
        if(empty($data)){
            throw new ApiV2Exception("未找到审核数据");
        }
        if(!empty($data['audit_at'])){
            throw new ApiV2Exception("已经审核过了");
        }
        $Inventory = PartsInventory::findOrFail($id);
        if(empty($Inventory)){
            throw new ApiV2Exception("配件不存在，不能申请审核");
        }
        if($Inventory['state'] == PartsInventory::STATE_SCRAP){
            throw new ApiV2Exception("配件已经报废，操作失败");
        }
        if($Inventory['state'] != PartsInventory::STATE_NEW_PRODUCT){
            throw new ApiV2Exception("配件不是新品入库，操作失败");
        }
        
        $uid = Auth::id();
        DB::beginTransaction(); 
        try{
            $FirstAuditReceive = FirstAuditReceive::findOrFail($data['id']);
            $AuditReceiveData = array('audit_uid'=>$uid,'audit_at'=>Carbon::now());
            $FirstAuditReceive->fill($AuditReceiveData)->save();

            $Parts = Parts::findOrFail($id);
            $Parts->fill(array('new_product'=>Parts::ONT_NEW))->save();

            $Inventory->fill(array('state' => PartsInventory::STATE_CANGKU,'user_id'=>$uid))->save();
            DB::commit();
        } catch (\Exception $e) {
            DB::rollback();
            throw $e;
        }
        
        return;
    }

    //获取配件id
    public function getpartsid($data){
        if(is_numeric($data)){
            return $data;
        }else{
            $Parts = Parts::query()->where('partsCode','=',$data)->first();
            return $Parts['id'];
        }
    }
   
}
